package com.blog.user.controller;


import com.blog.common.vo.JsonResult;
import com.blog.user.pojo.Consumer;
import com.blog.user.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author dc
 * @Classname ConsumerController
 * @Date 2020/7/30 19:54
 */
@RestController
@RequestMapping("/api/rest")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @PostMapping("/consumer/register")
    public JsonResult consumerRegister(@RequestBody Consumer consumer){
        JsonResult result = consumerService.saveConsumer(consumer);
        return result;
    }

    @PostMapping("/consumer/loginCheck")
    public JsonResult consumerLoginCheck(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        String ticket = null;
        if(cookies!=null&&cookies.length>0){
            for(Cookie cookie : cookies){
                if (cookie.getName()=="BLOG_TICKET"){
                    ticket=cookie.getValue();
                    break;
                }
            }
        }
        if(!StringUtils.isEmpty(ticket)){
            JsonResult result =consumerService.loginCheck(ticket);
            return result;
        }
        return JsonResult.fail();
    }

    @PostMapping("/consumer/login")
    public JsonResult consumerLogin(@RequestBody Map<String,Object> data, HttpServletResponse response) throws UnsupportedEncodingException {
        Boolean checked = (Boolean) data.get("checked");
        String rs=consumerService.loginHandle(data);
        if(rs=="用户名或密码错误！"){
            return JsonResult.fail(rs);
        }
        Cookie cookie = new Cookie("BLOG_TICKET", rs);
        //设定cookie权限,所有请求路径都可以访问
        cookie.setPath("/");
        if(checked) {
            //设定cookie的共享,在所有xx.com结尾的网站共享
            //cookie.setDomain("xx.com");
            //设定超时时间 一个小时
            cookie.setMaxAge(3600);
        }
        response.addCookie(cookie);
        return JsonResult.success();
    }

    @RequestMapping("/consumer/usernameCheck")
    public JsonResult consumerUsernameCheck(@RequestBody Map<String,String> data){
        JsonResult result = consumerService.checkUsername(data.get("username"));
        return result;
    }

    @RequestMapping("/consumer/out/{name}")
    public JsonResult consumerOut(@PathVariable("name") String name,HttpServletResponse response){
        if(name!=null & name.equals("BLOG_TICKET")){
            Cookie cookie = new Cookie("BLOG_TICKET",null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
}
